package com.ruoyi.quartz.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.quartz.race.init.ServiceHistory;
import com.ruoyi.quartz.race.init.ServiceTouzhu;
import com.ruoyi.quartz.race.init.ServiceTrend;
import com.ruoyi.quartz.race.model.RaceEntity;
import com.ruoyi.quartz.race.model.RaceStatusEntity;
import com.ruoyi.quartz.race.model.RaceTouzhu;
import com.ruoyi.quartz.race.model.RealTimeEntity;
import com.ruoyi.quartz.race.model.SetEntity;
import com.ruoyi.quartz.race.type.ServiceJc;
import com.ruoyi.quartz.util.JedisUtil;

import cn.hutool.core.util.StrUtil;
import us.codecraft.webmagic.Spider;

/**
 * 调度任务信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/schedule/rj")
public class SysRjController extends BaseController {

	@GetMapping("/listhistory")
	public List<Map<String, String>> listhistory(String typeid, String comp) {
		List<Map<String, String>> resultList = new ArrayList();
		for (int i = 0; i < 30; i++) {
			Calendar myTime = Calendar.getInstance();
			myTime.add(Calendar.DATE, -i);
			Date beforeM = myTime.getTime();
			String nday = new SimpleDateFormat("yyyy-MM-dd").format(beforeM);
			System.out.println(nday);
			Map<String, String> tm = new HashMap();
			tm.put("id", nday);
			resultList.add(tm);
		}
		return resultList;
	}

	@GetMapping("/stock")
	public List<Map> stock() {
		List<Map> resultList = new ArrayList();
		String json = JedisUtil.get("stock");
		if (StrUtil.isNotBlank(json)) {
			resultList = JSON.parseArray(json, Map.class);
		}
		return resultList;
	}

	@GetMapping("/rebf")
	public String rebf(String typeid, String history) {
		if (StrUtil.isBlank(history)) {
			history = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		}

		String url = "https://live.500.com?e=" + history;
		Spider.create(new ServiceJc()).addUrl(url).run();

		return "1";
	}

	@GetMapping("/recal")
	public String recal(String typeid, String history) {
		if (StrUtil.isBlank(history)) {
			history = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		}

		List<RaceEntity> resultList = JSON.parseArray(JedisUtil.get(typeid + ":" + history), RaceEntity.class);
		if (resultList != null && resultList.size() > 0) {
			ServiceTrend.processList(resultList);
			ServiceTouzhu.processList(resultList);
		}
		return "1";
	}

	@PostMapping("/setopt")
	public String setopt(SetEntity setEntity) {
		JedisUtil.set("setOpt:" + setEntity.getId(), JSON.toJSONString(setEntity));
		return "1";
	}

	/**
	 * 查询列表
	 */
	@GetMapping("/list")
	public List<RaceEntity> list(String typeid, String comp, String history) {
		if (StrUtil.isBlank(history)) {
			history = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		}
		return getList(typeid, comp, history);
	}

	/**
	 * 查询列表
	 */
	@GetMapping("/listsamehis")
	public List<RaceEntity> listsamehis(String id, String typeid, String comp) {
		// 获取最近10天的
		Calendar myTime = Calendar.getInstance();
		List<RaceEntity> alllist = new ArrayList();
		RaceEntity self = new RaceEntity();
		String realtimestr = JedisUtil.get("realtime:" + id);
		RealTimeEntity realtimeEntity = new RealTimeEntity();
		if (realtimestr != null && realtimestr.length() > 0) {
			realtimeEntity = JSON.parseObject(realtimestr, RealTimeEntity.class);
			String begasian = realtimeEntity.getBegas().split(",")[1];
			String endasian = realtimeEntity.getEndas().split(",")[1];
			for (int i = 0; i < 75; i++) {
				myTime.add(Calendar.DATE, -i);
				Date beforeM = myTime.getTime();
				String history = new SimpleDateFormat("yyyy-MM-dd").format(beforeM);
				List<RaceEntity> templist = getList(typeid, comp, history);
				if (templist != null && templist.size() > 0) {
					for (int m = 0; m < templist.size(); m++) {
						if (templist.get(m).getId().equals(id)) {
							self = templist.get(m);
						} else {
							if (templist.get(m).getRealTime().getBegas().contains("," + begasian + ",")
									&& templist.get(m).getRealTime().getEndas().contains("," + endasian + ",")) {
								alllist.add(templist.get(m));
							}
						}
					}
				}
			}
		}
		alllist.add(0, self);
		return alllist;
	}

	public static void main(String[] args) {
		// List<RaceEntity> list = getList("1", "", "");
		// System.out.println(JSON.toJSONString(list));
		// 获取最近10天的
		String id = "1100523";
		String typeid = "raceList";
		String comp = "0";
		Calendar myTime = Calendar.getInstance();
		List<RaceEntity> alllist = new ArrayList();
		String realtimestr = JedisUtil.get("realtime:" + id);
		RealTimeEntity realtimeEntity = new RealTimeEntity();
		if (realtimestr != null && realtimestr.length() > 0) {
			realtimeEntity = JSON.parseObject(realtimestr, RealTimeEntity.class);
			String begasian = realtimeEntity.getBegas().split(",")[1];
			for (int i = 1; i < 10; i++) {
				myTime.add(Calendar.DATE, -i);
				Date beforeM = myTime.getTime();
				String history = new SimpleDateFormat("yyyyMMdd").format(beforeM);
				List<RaceEntity> templist = getList(typeid, comp, history);
				if (templist != null && templist.size() > 0) {
					for (int m = 0; m < templist.size(); m++) {
						if (templist.get(m).getRealTime().getBegas().contains("," + begasian + ",")) {
							alllist.add(templist.get(m));
						}
					}
				}
			}
		}
		System.out.println(alllist.size());
	}

	private static List<RaceEntity> getList(String typeid, String comp, String history) {
		List<RaceEntity> resultList = JSON.parseArray(JedisUtil.get(typeid + ":" + history), RaceEntity.class);
		if (resultList != null && resultList.size() > 0) {
			for (int i = 0; i < resultList.size(); i++) {
				RaceEntity temp = resultList.get(i);
				RaceTouzhu touzhuEntity = new RaceTouzhu();
				RaceStatusEntity statusEntity = new RaceStatusEntity();
				RealTimeEntity realtimeEntity = new RealTimeEntity();
				SetEntity setEntity = new SetEntity();
				setEntity.setId(temp.getId());
				//
				String tzstr = JedisUtil.get("touzhu:" + temp.getId());
				if (tzstr != null && tzstr.length() > 0) {
					touzhuEntity = JSON.parseObject(tzstr, RaceTouzhu.class);
				}

				String jsonstr = JedisUtil.get("trend:" + temp.getId());
				if (jsonstr != null && jsonstr.length() > 0) {
					statusEntity = JSON.parseObject(jsonstr, RaceStatusEntity.class);
				}

				String realtimestr = JedisUtil.get("realtime:" + temp.getId());
				if (realtimestr != null && realtimestr.length() > 0) {
					realtimeEntity = JSON.parseObject(realtimestr, RealTimeEntity.class);
				}

				String setstr = JedisUtil.get("setOpt:" + temp.getId());
				if (setstr != null && setstr.length() > 0) {
					setEntity = JSON.parseObject(setstr, SetEntity.class);
				}

				//
				temp.setSetEntity(setEntity);
				temp.setTouzhuEntity(touzhuEntity);
				temp.setStatusEntity(statusEntity);
				temp.setRealTime(realtimeEntity);
			}
		}
		return resultList;
	}
}
